#!/bin/bash
# Download and statically build tools needed for cactus-refmap and cactus-graphmap.  They are
# - minimap2
# - minigraph
# - gfatools
# - dna-brnn
# - cactus-gfa-tools
# - samtools
# - bedtools
# The following tools are included to export and work with pangenome graph formats
# - hal2vg
# - vg
# If, updating any versions here, update the release notes as well
#
# all binaries get copied into cactus/bin

# set this to one to make sure everything gets built statically (necessary for binary release)
STATIC_CHECK=$1

set -beEu -o pipefail

pangenomeBuildDir=$(realpath -m build-pangenome-tools)
binDir=$(pwd)/bin
# just use cactusRootPath for now
dataDir=$(pwd)/src/cactus
CWD=$(pwd)
# works on MacOS and Linux
if [ -z ${numcpu+x} ]; then
	 numcpu=$(getconf _NPROCESSORS_ONLN)
fi

set -x
rm -rf ${pangenomeBuildDir}
mkdir -p ${pangenomeBuildDir}
mkdir -p ${binDir}

# minimap2
cd ${pangenomeBuildDir}
git clone https://github.com/lh3/minimap2.git
cd minimap2
git checkout 581f2d7123f651d04c9e103b1c7ea8f7051e909a
# hack in flags support
sed -i Makefile -e 's/CFLAGS=/CFLAGS+=/'
make -j ${numcpu}
if [[ $STATIC_CHECK -ne 1 || $(ldd minimap2 | grep so | wc -l) -eq 0 ]]
then
	 mv minimap2 ${binDir}
else
	 exit 1
fi

# minigraph
cd ${pangenomeBuildDir}
git clone https://github.com/lh3/minigraph.git
pushd minigraph
git checkout 7bdfc6aed2857807d901d910134b86b947b5b087
# hack in flags support
sed -i Makefile -e 's/CFLAGS=/CFLAGS+=/'
make -j ${numcpu}
if [[ $STATIC_CHECK -ne 1 || $(ldd minigraph | grep so | wc -l) -eq 0 ]]
then
	 mv minigraph ${binDir}
else
	 exit 1
fi

# gfatools
cd ${pangenomeBuildDir}
git clone https://github.com/lh3/gfatools.git
cd gfatools
git checkout 195273ef67536c36fd24242e9bb0f17053f8ab8b
# hack in flags support
sed -i Makefile -e 's/CFLAGS=/CFLAGS+=/'
make -j ${numcpu}
if [[ $STATIC_CHECK -ne 1 || $(ldd gfatools | grep so | wc -l) -eq 0 ]]
then
	 mv gfatools ${binDir}
else
	 exit 1
fi

#samtools
cd ${pangenomeBuildDir}
wget -q https://github.com/samtools/samtools/releases/download/1.11/samtools-1.11.tar.bz2
tar -xf samtools-1.11.tar.bz2
cd samtools-1.11
SAMTOOLS_CONFIG_OPTS=""
if [[ $STATIC_CHECK -eq 1 ]]
then
	 SAMTOOLS_CONFIG_OPTS="--disable-shared --enable-static"
fi
./configure --without-curses --disable-libcurl --enable-configure-htslib $SAMTOOLS_CONFIG_OPTS
make -j ${numcpu}
if [[ $STATIC_CHECK -ne 1 || $(ldd samtools | grep so | wc -l) -eq 0 ]]
then
	 mv samtools ${binDir}
else
	 exit 1
fi
cd htslib-1.11
make -j ${numcpu} tabix
make -j ${numcpu} bgzip
if [[ $STATIC_CHECK -ne 1 || $(ldd tabix | grep so | wc -l) -eq 0 ]]
then
	 mv tabix ${binDir}
else
	 exit 1
fi
if [[ $STATIC_CHECK -ne 1 || $(ldd bgzip | grep so | wc -l) -eq 0 ]]
then
	 mv bgzip ${binDir}
else
	 exit 1
fi

#bedtools
cd ${pangenomeBuildDir}
wget -q https://github.com/arq5x/bedtools2/releases/download/v2.29.1/bedtools-2.29.1.tar.gz
tar -zxf bedtools-2.29.1.tar.gz
cd bedtools2
if [[ $STATIC_CHECK -ne 1 ]]
then
	 make -j ${numcpu}
	 mv bin/bedtools ${binDir}/bedtools
else
	 make static -j ${numcpu}
	 if [[ $STATIC_CHECK -ne 1 || $(ldd bin/bedtools.static | grep so | wc -l) -eq 0 ]]
	 then
		  mv bin/bedtools.static ${binDir}/bedtools
	 else
		  exit 1
	 fi
fi

# dna-brnn
cd ${pangenomeBuildDir}
git clone https://github.com/lh3/dna-nn.git
cd dna-nn
git checkout 2e6d242ae339457b985f50086e85194c3ce418b1
# hack in flags support
sed -i Makefile -e 's/CFLAGS=/CFLAGS+=/' -e 's/LIBS=/LIBS+=/'
if [ -z ${CFLAGS+x} ]
then
	 make -j ${numcpu}
else
	 LIBS="${CFLAGS}" make -j ${numcpu}
fi
if [[ $STATIC_CHECK -ne 1 || $(ldd dna-brnn | grep so | wc -l) -eq 0 ]]
then
	 mv dna-brnn ${binDir}
	 cp models/attcc-alpha.knm ${dataDir}
else
	 exit 1
fi

# cactus-gfa-tools
cd ${pangenomeBuildDir}
git clone https://github.com/ComparativeGenomicsToolkit/cactus-gfa-tools.git
cd cactus-gfa-tools
git checkout 06eaa932bf44c7017b4c56775af9dbf801fb3405
make -j ${numcpu}
if [[ $STATIC_CHECK -ne 1 || $(ldd paf2lastz | grep so | wc -l) -eq 0 ]]
then
	 mv paf2lastz ${binDir}
else
	 exit 1
fi
if [[ $STATIC_CHECK -ne 1 || $(ldd mzgaf2paf | grep so | wc -l) -eq 0 ]]
then
	 mv mzgaf2paf ${binDir}
else
	 exit 1
fi
if [[ $STATIC_CHECK -ne 1 || $(ldd rgfa-split | grep so | wc -l) -eq 0 ]]
then
	 mv rgfa-split ${binDir}
else
	 exit 1
fi
if [[ $STATIC_CHECK -ne 1 || $(ldd rgfa2paf | grep so | wc -l) -eq 0 ]]
then
	 mv rgfa2paf ${binDir}
else
	 exit 1
fi
if [[ $STATIC_CHECK -ne 1 || $(ldd pafcoverage | grep so | wc -l) -eq 0 ]]
then
	 mv pafcoverage ${binDir}
else
	 exit 1
fi
if [[ $STATIC_CHECK -ne 1 || $(ldd pafmask | grep so | wc -l) -eq 0 ]]
then
	 mv pafmask ${binDir}
else
	 exit 1
fi
if [[ $STATIC_CHECK -ne 1 || $(ldd paf2stable | grep so | wc -l) -eq 0 ]]
then
	 mv paf2stable ${binDir}
else
	 exit 1
fi

# hal2vg
cd ${pangenomeBuildDir}
wget -q https://github.com/ComparativeGenomicsToolkit/hal2vg/releases/download/v1.0.15/hal2vg
chmod +x hal2vg
if [[ $STATIC_CHECK -ne 1 || $(ldd hal2vg | grep so | wc -l) -eq 0 ]]
then
	 mv hal2vg ${binDir}
else
	 exit 1
fi
# clip-vg
cd ${pangenomeBuildDir}
wget -q https://github.com/ComparativeGenomicsToolkit/hal2vg/releases/download/v1.0.15/clip-vg
chmod +x clip-vg
if [[ $STATIC_CHECK -ne 1 || $(ldd clip-vg | grep so | wc -l) -eq 0 ]]
then
	 mv clip-vg ${binDir}
else
	 exit 1
fi
# halRemoveDupes
cd ${pangenomeBuildDir}
wget -q https://github.com/ComparativeGenomicsToolkit/hal2vg/releases/download/v1.0.15/halRemoveDupes
chmod +x halRemoveDupes
if [[ $STATIC_CHECK -ne 1 || $(ldd halRemoveDupes | grep so | wc -l) -eq 0 ]]
then
	 mv halRemoveDupes ${binDir}
else
	 exit 1
fi
# halMergeChroms
cd ${pangenomeBuildDir}
wget -q https://github.com/ComparativeGenomicsToolkit/hal2vg/releases/download/v1.0.15/halMergeChroms
chmod +x halMergeChroms
if [[ $STATIC_CHECK -ne 1 || $(ldd halMergeChroms | grep so | wc -l) -eq 0 ]]
then
	 mv halMergeChroms ${binDir}
else
	 exit 1
fi

# halUnclip
cd ${pangenomeBuildDir}
wget -q https://github.com/ComparativeGenomicsToolkit/hal2vg/releases/download/v1.0.15/halUnclip
chmod +x halUnclip
if [[ $STATIC_CHECK -ne 1 || $(ldd halUnclip | grep so | wc -l) -eq 0 ]]
then
	 mv halUnclip ${binDir}
else
	 exit 1
fi

# vg
cd ${pangenomeBuildDir}
#tempoarary hack until 1.35.0 is actually released
wget -q http://public.gi.ucsc.edu/~hickey/vg
#wget -q https://github.com/vgteam/vg/releases/download/v1.35.0/vg
chmod +x vg
if [[ $STATIC_CHECK -ne 1 || $(ldd vg | grep so | wc -l) -eq 0 ]]
then
	 mv vg ${binDir}
else
	 exit 1
fi	 

# gfaffix
cd ${pangenomeBuildDir}
wget -q https://github.com/marschall-lab/GFAffix/releases/download/0.1.2/GFAffix-0.1.2_linux_x86_64.tar.gz
tar xzf GFAffix-0.1.2_linux_x86_64.tar.gz
chmod +x GFAffix-0.1.2/gfaffix
if [[ $STATIC_CHECK -ne 1 || $(ldd GFAffix-0.1.2/gfaffix | grep so | wc -l) -eq 0 ]]
then
	 mv GFAffix-0.1.2/gfaffix ${binDir}
else
	 exit 1
fi

cd ${CWD}
rm -rf ${pangenomeBuildDir}

set +x
echo ""
echo "--------------------------------------------------"
echo "(re)run pip install -U . to install dna-brnn model"
echo "--------------------------------------------------"
